package com.adcc.ags.gms.gw.service;

import com.adcc.ags.gms.core.entity.GWLogLevel;
import com.adcc.ags.gms.core.mapper.GWLogMapper;
import com.adcc.ags.gms.core.util.LogFactory;
import com.adcc.ags.gms.gw.vo.GWLogVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("GMSLogService")
public class GMSLogServiceImpl implements GMSLogService {

    @Autowired
    private GWLogMapper gwLogMapper;

    @Autowired
    private LogFactory log;

    @Override
    public Optional<PageInfo<GWLogVO>> findAllLogs(String logLevel, String queryOperateName, String dateStart, String dateEnd, String queryKeyword, int curPage,int pageSize) {
        try {
            PageHelper.startPage(curPage, pageSize);
            List<GWLogVO> list = gwLogMapper.findAll(logLevel, queryOperateName, dateStart, dateEnd, queryKeyword);
            PageInfo<GWLogVO> pageInfo = new PageInfo<>(list);
            return Optional.of(pageInfo);
        } catch (Exception ex) {
            log.error(GMSLogService.class, "GMSLog", GWLogLevel.ALARM, "findAllLogs() error", ex);
            return Optional.absent();
        }
    }

    @Override
    public Optional<List<GWLogVO>> findMonitorLogs(int logCount) {
        try {
            List<GWLogVO> list = gwLogMapper.findMonitorLogs(logCount);
            return Optional.of(list);
        } catch (Exception ex) {
            log.error(GMSLogService.class, "GMSLog", GWLogLevel.ALARM, "findMonitorLogs() error", ex);
            return Optional.absent();
        }
    }

    @Override
    public void deleteLog() {
        try {
            gwLogMapper.deleteLog();
        } catch (Exception ex) {
            log.error(GMSLogService.class, "deleteLog() error", ex);
        }
    }

}
